import appRouter from '@/router/router';

const app = {
  state: {
    menuList: [],
    currentPaths: [],
    menuRoot: null
  },
  mutations: {
    setMenus(state, menuPrivileges) {
      let menuList = [];
      menuPrivileges.forEach(x => {
        let childrenList = [];
        x.children.forEach(children => {
          appRouter.children.forEach(router => {
            if (router.path == (`${process.env.VUE_APP_VIRTUAL_PATH}/${children.path}`)) {
              childrenList.push({
                menuId: children.menuId,
                path: router.path,
                component: router.component,
                // name: router.path,
                icon: children.icon,
                title: children.title
              })
            }
          })

        })
        menuList.push({
          rid: x.menuId,
          title: x.title,
          children: childrenList,
          icon: x.icon,
          component: appRouter.component,
          path: appRouter.path
        })
      })
      state.menuList = menuList;
    },
    setCurrentPaths(state, path) {
      // parentPath
      let currentPath = [{
        path: "/main.html",
        hraf: "",
        title: "首页"
      }];
      state.menuList.forEach(x => {
        x.children.forEach(x2 => {
          if (x2.path == path) {
            currentPath.push(x);
            currentPath.push(x2);
          }
        })
      })
      if (currentPath) { //存在,记录
        // state.commit('currentPaths', currentPath);
        state.currentPaths.push(currentPath);
      } else { //搜索其他路由

      }

    },
    setMenuRoot(state, menuRoot) {
      state.menuRoot = menuRoot;
    }
  }
};

export default app;